﻿using DevExpress.XtraGrid.Views.BandedGrid;
using DLL;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.InfantBaby
{
    public partial class InfantProducAfterList : Form
    {
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo Patientinfo;
        public InfantProducAfterList(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            StatDate.DateTime = DateTime.Now.AddDays(-30);
            EndDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            this.LoginUser = loginUser;
            this.Patientinfo = patientInfo;
            LoadData("", "");
            SetBandGridView();
        }
        #region GridView设置
        //GridView设置
        private void SetBandGridView()
        {
            foreach (GridBand gridBand in this.bandedGridView1.Bands)//设置居中
            {
                foreach (GridBand gridBandSecond in gridBand.Children)
                {
                    gridBandSecond.OptionsBand.FixedWidth = true;
                    gridBandSecond.AppearanceHeader.Options.UseTextOptions = true;
                    gridBandSecond.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    gridBandSecond.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                }
                gridBand.OptionsBand.FixedWidth = true;
                gridBand.AppearanceHeader.Options.UseTextOptions = true;
                gridBand.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridBand.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }
        }
        #endregion

        #region 查询
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string StatDate = this.StatDate.Text;
            string EndDate = this.EndDate.Text;

            if (string.IsNullOrEmpty(StatDate) || string.IsNullOrEmpty(EndDate))
            {
                MessageBox.Show("查询日期不能为空！");
            }
            LoadData(StatDate, EndDate);
        }
        #endregion

        #region 新增
        private void btnAdd_Click(object sender, EventArgs e)
        {
            InfantProducAfterEdit InfantProducAfterEdit = new InfantProducAfterEdit(LoginUser, Patientinfo);
            InfantProducAfterEdit.ShowDialog();
            if (InfantProducAfterEdit.DialogResult != DialogResult.OK) return;
            LoadData("", "");
        }
        #endregion

        #region 修改
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
            InfantProducAfterEdit addForm = new InfantProducAfterEdit(LoginUser, Patientinfo);
            addForm.ShowDialog();
            if (addForm.DialogResult != DialogResult.OK) return;
            LoadData("", "");
        }
        #endregion

        #region 打印
        private void btnPrint_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region 删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                LoadData("","");
            }
        }
        #endregion

        #region LoadData显示
        private void LoadData(string StatDate, string EndDate)
        {
            if (this.Patientinfo == null) return;
            string patientid = this.Patientinfo.PATIENT_ID;//住院号
            int VisitId = this.Patientinfo.VISIT_ID;//住院次数
            int FormId = (int)FormType.FetationAfter;//18006  //表单ID
            string select_sql = @"SELECT  * ,
                                    
'' AS ChanCi,
'' AS XingBie,
'' AS RenShenZhouShu,
'' AS ChuShengShiJian,
'' AS TaiWei,
'' AS FenMianFangShi,
'' AS tizhong,
'' AS shenchang,
'' AS bingfazheng,
'' AS hbsag,
'' AS yangshui,

'' AS taiergongneijiongpowu,
'' AS taiergongneijiongpoyou,
'' AS taiergongneijiongpoText,

'' AS qidaiyichang1,
'' AS qidaiyichang2,
'' AS kainaishijian,
'' AS meifenzhongxinlv4,
'' AS huxi4,
'' AS jizhangli4,
'' AS houfanshe4,
'' AS pifuyanse4,
'' AS zongpingfenshu4,
'' AS jieshengzhe,
'' AS dianyanzhe,
'' AS tiwen,
'' AS pifu,
'' AS toubu,
'' AS xiongbu,
'' AS fubu,
'' AS chubuyinxiang,
'' AS jianchazhe,
'' AS zhiliaojingguo,
'' AS yisheng,
'' AS chuyuanjiancha,
'' AS chuyuanzhenduan,
'' AS zhiliaozhuangui,
'' AS chuyuanyizhu,
'' AS chuyuanriqi,

'' AS renshenhebingzheng1,
'' AS renshenhebingzheng2,
'' AS renshenhebingzheng3,
'' AS renshenhebingzheng4,
'' AS renshenhebingzheng5,
'' AS renshenhebingzheng6,
'' AS renshenhebingzheng7,
'' AS renshenhebingzheng8,
'' AS renshenhebingzheng9,
'' AS renshenhebingzheng10,
'' AS renshenhebingzheng11,
'' AS renshenhebingzheng12,
'' AS renshenhebingzheng13,
'' AS renshenhebingzheng14,
'' AS renshenhebingzheng15,
'' AS renshenhebingzheng16,
'' AS renshenhebingzheng17,
'' AS renshenhebingzheng18,
'' AS renshenhebingzheng19,
'' AS renshenhebingzhengText,

'' AS zaoposhuiyou,
'' AS zaoposhuiwu,
'' AS ganranyou,
'' AS ganranwu,

'' AS zhichanwu,
'' AS zhichanyou,
'' AS zhichanText,

'' AS taipanyichangyou,
'' AS taipanyichangwu,


'' AS Id


                            FROM    dbo.NursingFormRecord(NOLOCK)
                            WHERE   FormId = {0} 
                                    AND patientid = '{1}' 
                                    AND VisitId = {2} {3} 
                            ORDER BY [AssessmentDate] DESC";

            string whereList = string.Empty;
            if (StatDate != "" && EndDate != "")
            {
                StatDate += " 00:00:00";
                EndDate += " 23:59:59";
                whereList = string.Format(" AND [AssessmentDate]>=CONVERT(DATETIME,'{0}') AND  [AssessmentDate]<=CONVERT(DATETIME,'{1}')", StatDate, EndDate);
            }
            select_sql = string.Format(select_sql, FormId, patientid, VisitId, whereList);
            DataTable dt = (SqlHelper.ExecuteDataset(select_sql, CommandType.Text)).Tables[0];            
            DataTable dtt = dt.Clone();
            gridControl1.DataSource = dt;
            if (dt.Rows.Count <= 0) return;
            string theTrue = "√";
            foreach (DataRow dr in dt.Rows)
            {
                var NursingFormData = Convert.ToString(dr["NursingFormData"]);
                Model.InfantProducAfter fd = new Model.InfantProducAfter();
                if (string.IsNullOrEmpty(NursingFormData)) continue;
                fd = XmlUtil.XMLToObject<Model.InfantProducAfter>(NursingFormData, Encoding.UTF8);
                DataRow newRow = dtt.NewRow();

                newRow["ChanCi"] = fd.ChanCi;
                newRow["XingBie"] = fd.XingBie;
                newRow["RenShenZhouShu"] = fd.RenShenZhouShu;
                newRow["ChuShengShiJian"] = fd.ChuShengShiJian;
                newRow["TaiWei"] = fd.TaiWei;
                newRow["FenMianFangShi"] = fd.FenMianFangShi;
                newRow["tizhong"] = fd.TiZhong;
                newRow["shenchang"] = fd.ShenChang;

                newRow["bingfazheng"] = fd.BingFaZheng;
                newRow["hbsag"] = fd.HBsAg;
                newRow["yangshui"] = fd.YangShui;

                newRow["taiergongneijiongpowu"] = fd.TaiErGongNeiJiongPo.Equals("0") ? theTrue : string.Empty;
                newRow["taiergongneijiongpoyou"] = fd.TaiErGongNeiJiongPo.Equals("1") ? theTrue : string.Empty;
                newRow["taiergongneijiongpoText"] = fd.TaiErGongNeiJiongPoText;

                newRow["qidaiyichang1"] = fd.QiDaiYiChang.Equals("0") ? theTrue : string.Empty;
                newRow["qidaiyichang2"] = fd.QiDaiYiChang.Equals("1") ? theTrue : string.Empty;
                newRow["kainaishijian"] = fd.KaiNaiShiJian;
                newRow["meifenzhongxinlv4"] = fd.MeiFenZhongXinLv4;
                newRow["huxi4"] = fd.HuXi4;
                newRow["jizhangli4"] = fd.JiZhangLi4;
                newRow["houfanshe4"] = fd.HouFanShe4;
                newRow["pifuyanse4"] = fd.PiFuYanSe4;
                newRow["zongpingfenshu4"] = fd.ZongPingFenShu4;
                newRow["jieshengzhe"] = fd.JieShengZhe;
                newRow["dianyanzhe"] = fd.DianYanZhe;
                 //

                newRow["renshenhebingzheng1"] = fd.RenShenHeBingZheng.Split('#')[0].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng2"] = fd.RenShenHeBingZheng.Split('#')[1].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng3"] = fd.RenShenHeBingZheng.Split('#')[2].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng4"] = fd.RenShenHeBingZheng.Split('#')[3].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng5"] = fd.RenShenHeBingZheng.Split('#')[4].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng6"] = fd.RenShenHeBingZheng.Split('#')[5].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng7"] = fd.RenShenHeBingZheng.Split('#')[6].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng8"] = fd.RenShenHeBingZheng.Split('#')[7].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng9"] = fd.RenShenHeBingZheng.Split('#')[8].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng10"] = fd.RenShenHeBingZheng.Split('#')[9].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng11"] = fd.RenShenHeBingZheng.Split('#')[10].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng12"] = fd.RenShenHeBingZheng.Split('#')[11].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng13"] = fd.RenShenHeBingZheng.Split('#')[12].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng14"] = fd.RenShenHeBingZheng.Split('#')[13].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng15"] = fd.RenShenHeBingZheng.Split('#')[14].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng16"] = fd.RenShenHeBingZheng.Split('#')[15].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng17"] = fd.RenShenHeBingZheng.Split('#')[16].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng18"] = fd.RenShenHeBingZheng.Split('#')[17].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzheng19"] = fd.RenShenHeBingZheng.Split('#')[18].Equals("1") ? theTrue : string.Empty;
                newRow["renshenhebingzhengText"] = fd.QiTaNeiWaiKeJiBing;

                newRow["zaoposhuiyou"] = fd.ZaoPoShui.Equals("0") ? theTrue : string.Empty;
                newRow["zaoposhuiwu"] = fd.ZaoPoShui.Equals("1") ? theTrue : string.Empty;
                newRow["ganranyou"] = fd.GanRan.Equals("0") ? theTrue : string.Empty;
                newRow["ganranwu"] = fd.GanRan.Equals("1") ? theTrue : string.Empty;

                newRow["zhichanwu"] = fd.ZhiChan.Equals("0") ? theTrue : string.Empty;
                newRow["zhichanyou"] = fd.GanRan.Equals("1") ? theTrue : string.Empty;
                newRow["zhichanText"] = fd.ZhiChanText;

                newRow["taipanyichangyou"] = fd.TaiPanYiChang.Equals("0") ? theTrue : string.Empty;
                newRow["taipanyichangwu"] = fd.TaiPanYiChang.Equals("1") ? theTrue : string.Empty;


                //ID隐藏，双击触发事件
                newRow["Id"] = dr["ID"];
                dtt.Rows.Add(newRow);
            }

            gridControl1.DataSource = dtt;
        }
        #endregion

        #region 双击事件
        private void bandedGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
            InfantProducAfterEdit addForm = new InfantProducAfterEdit(LoginUser, Patientinfo);
            addForm.ShowDialog();
            if (addForm.DialogResult != DialogResult.OK) return;
            LoadData("", "");
        }
        #endregion
    }
}